
package spatialmarketplace.cem.action;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpSession;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.util.Log;
import spatialmarketplace.cem.dao.implementation.ResourceDaoImpl;
import spatialmarketplace.cem.model.Group;
import spatialmarketplace.cem.model.Resource;

/**
 * 
 * Display resource data and share resource to other groups
 * 
 * @author Hung
 */
public class ResourceSharingActionBean extends BaseActionBean {

    private static final Log logger = Log.getInstance(ResourceDetailActionBean.class);
    private static final String FORM = "/WEB-INF/jsp/resource_sharing.jsp";
    /**
     * Store current resource information that is selected
     */
    protected Resource resource;
    List<Group> groupsUnSharing = new ArrayList<Group>();

    /**
     * Get resource data
     *
     * @return
     */
    public Resource getResource() {
        return resource;
    }

    /**
     * Set data to resource
     *
     * @param resource
     */
    public void setResource(Resource resource) {
        this.resource = resource;
    }

    public List<Group> getGroupsUnSharing() {
        return groupsUnSharing;
    }

    public void setGroupsUnSharing(List<Group> groupsUnSharing) {
        this.groupsUnSharing = groupsUnSharing;
    }

    /**
     * get all information about resource in database and display it to html
     *
     * @param resourceId Integer value
     */
    protected void loadResourceDetail(Integer resourceId) {

        resource = ResourceDaoImpl.getInstance().read_Resource(resourceId);
    }

    /**
     * Load all information in the first opening web page
     *
     * @return
     */
    @DefaultHandler
    public Resolution form() {

        try {
            
            Integer resourceId = Integer.valueOf(getContext().getRequest().getParameter("resourceId"));

            loadResourceDetail(resourceId);

            groupsUnSharing = loadGroupsUnSharing();
            getContext().setGroupOrResourceId(resourceId);

        } catch (Exception e) {
            Integer resourceId = getContext().getGroupOrResourceId();
            loadResourceDetail(resourceId);
            groupsUnSharing = loadGroupsUnSharing();
        }
        return new ForwardResolution(FORM);
    }

    /**
     * Share the resource to other groups
     * @return none
     */
    public Resolution shareGroupResource() {
        Integer resourceId = Integer.valueOf(getContext().getRequest().getParameter("resourceId"));
        Integer groupId = Integer.valueOf(getContext().getRequest().getParameter("groupId"));
        ResourceDaoImpl.getInstance().insertGroupShareResource(groupId, resourceId);

        getContext().setGroupOrResourceId(resourceId);
        return new RedirectResolution(ResourceSharingActionBean.class);
    }

    /**
     * get all groups that have not been shared the resource
     *
     * @return list of Group
     */
    public List<Group> loadGroupsUnSharing() {
        Integer resourceId = resource.getResourceId();
        Integer userId = getContext().getUserId();
        try {
            groupsUnSharing = ResourceDaoImpl.getInstance().listGroupsUnSharing(resourceId,userId);

        } catch (Exception e) {
        }
        return groupsUnSharing;
    }
}
